/**
 * @param {Array} ts 时间栈
 * @param {Object} keyline 基准阈值
 */
export function binarySearch (ts, keyline) {
  const early = 0
  const late = ts.length
  if (keyline >= ts[late - 1]) {
    return ts[late - 1]
  }
  if (keyline < ts[early]) {
    return -1
  }
  const mid = Number.parseInt((early + late) / 2)
  if (ts[mid] > keyline) {
    ts = ts.slice(0, mid)
    return binarySearch(ts, keyline)
  }
  if (ts[mid] < keyline) {
    ts = ts.slice(mid)
    return binarySearch(ts, keyline)
  }
  return ts[mid]
}
